<?php
class Mailbox extends CS_Controller
{
    function _init()
    {
        $this->load->library('pagination');
        $this->load->model('mailbox_model', 'mailbox');
        $this->load->model('user_type_model', 'user_type');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('mailbox/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('mailbox/grid');
        $config['total_rows'] = $this->mailbox->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['type'] = array(1=>'站内信', 2=>'系统消息', 4=>'订单');
        $data['user_type'] = $this->user_type->get_list('Y', array('2', '16', '8192', '134217728'));
        $this->load->view('mailbox/grid', $data);
    }
    
    public function add()
    {
        //$data['type'] = array(1=>'站内信', 2=>'系统消息', 4=>'订单');
        $data['user_type'] = $this->user_type->get_list('Y', array('2', '16', '8192', '134217728'));
        $this->load->view('mailbox/add', $data);
    }
    
    public function addPost()
    {
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('mailbox/add', '', $error);
        }
        $this->db->trans_start();
        $resultId = $this->mailbox->insertMailbox($this->input->post());
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('mailbox/grid', '', '保存成功！');
        } else {
            $this->error('mailbox/add', '', '保存失败！');
        }
    }
    
    public function edit($id)
    {
        $result = $this->mailbox->findById($id);
        if($result->num_rows() <= 0) {
            $this->redirect('mailbox/grid');
        }
        $data['mailbox'] = $result->row();
        $data['user_type'] = $this->user_type->get_list('Y', array('2', '16', '8192', '134217728'));
        $this->load->view('mailbox/edit', $data);
    }
    
    public function editPost()
    {
        $id = $this->input->post('id');
        $error = $this->validate();
    
        if (!empty($error)) {
            $this->error('mailbox/edit', $id, $error);
        }
    
        $this->db->trans_start();
        $resultId = $this->mailbox->updateMailbox($this->input->post());
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('mailbox/grid', '', '保存成功！');
        } else {
            $this->error('mailbox/edit', $id, '保存失败！');
        }
    }
    
    public function delete($id)
    {
        $is_delete = $this->mailbox->deleteById($id);
    
        if ($is_delete) {
            $this->success('mailbox/grid', '', '删除成功！');
        } else {
            $this->error('mailbox/grid', '', '删除失败！');
        }
    }
    
    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('user_type'))) {
            $error[] = '接收者不能为空';
        }
        if ($this->validateParam($this->input->post('title'))) {
            $error[] = '主题名称不能为空';
        }
        if ($this->validateParam($this->input->post('content'))) {
            $error[] = '内容不可为空';
        }
        return $error;
    }
}
